package site.cege;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PDFUtil {

    public static void main(String[] args) throws Exception {
        //new PDFUtil().buildTable();
        // buildHorizontalTable();
        testCreateTable();
    }

    public static void testCreateTable() throws Exception{

        Font pdfPTableFont = new Font(getBaseFont(), 10, Font.NORMAL);

        //生成水平表格
        Map<String, Object> data = buildTableData();
        String jsonFile = PDFUtil.class.getResource("/pdf.json").getFile();
        System.out.println("JSON-File==>" + jsonFile);
        PdfPTable table = PDFJSONUtil.buildTable(pdfPTableFont, jsonFile, data);

        //生成垂直表格
        List<Map<String, Object>> verticalData = buildverticalTableData();
        Map<String, Object> aggregateData = buildverticalAggregateData();
        String hjsonFile = PDFUtil.class.getResource("/pdf-vertical.json").getFile();
        System.out.println("JSON-File==>" + hjsonFile);
        PdfPTable verticalTable = PDFJSONUtil.buildVerticalTable(pdfPTableFont, hjsonFile, verticalData, aggregateData);

        File pdfFile = new File("C:\\tmp_floder\\table.pdf");
        writeToFile(pdfFile, table, verticalTable);
    }

    public static void testRowSpan()throws Exception{
        PdfPTable table = new PdfPTable(6);
        table.setSpacingBefore(20f);
        for (int i = 0; i < 36; i++) {
            PdfPCell pdfPCell = new PdfPCell(new Phrase("data-->"+i));
            pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            if(i == 0 || i == 1 || i == 2){
                pdfPCell.setRowspan(2);
            }
            table.addCell(pdfPCell);
        }
        writeToFile(new File("C:\\tmp_floder\\out-rowspan-2.pdf"), table);
    }

    public static void testPDFJSONUtil() throws Exception {
        Font pdfPTableFont = new Font(getBaseFont(), 10, Font.NORMAL);

        Map<String, Object> data = buildTableData();
        String jsonFile = PDFUtil.class.getResource("/pdf.json").getFile();
        System.out.println("JSON-File==>" + jsonFile);
        PdfPTable table = PDFJSONUtil.buildTable(pdfPTableFont, jsonFile, data);

        File pdfFile = new File("C:\\tmp_floder\\out-new.pdf");
        writeToFile(pdfFile, table);

    }
    public static Map<String, Object> buildTableData(){
        Map<String, Object> data = new HashMap<>();
        data.put("custom", "张三");
        data.put("partCompany", "北京特性行动小组");
        data.put("mastCompany", "TB000007");
        data.put("order", "XB000001");
        data.put("partOrder", "PO111001");
        data.put("flight", "北京南方航空");
        data.put("send", "意大香");
        data.put("arrive", "张家口");
        data.put("source", "上海转");
        data.put("service", "一次性服务");
        data.put("number", "200");
        data.put("weight", "13.5kg");
        data.put("volume", "23m");
        data.put("waybillWeight", "2kg");
        data.put("chargeWeight", "1kg");
        data.put("payWeight", "0.5kg");
        data.put("totalIncome", "500RMB");
        data.put("totalCost", "400.05RMB");
        data.put("totalEarnings", "200.05RMB");
        data.put("unitWeight", "20kg");
        data.put("freight", "2kg");
        data.put("mark", "好好运输好好打鸟");
        return data;
    }

    public static void buildverticalTable()throws Exception{
        List<Map<String, Object>> data = buildverticalTableData();
        Map<String, Object> aggregateData = buildverticalAggregateData();

        Font pdfPTableFont = new Font(getBaseFont(), 10, Font.NORMAL);

        String jsonFile = PDFUtil.class.getResource("/pdf-vertical.json").getFile();
        System.out.println("JSON-File==>" + jsonFile);
        PdfPTable table = PDFJSONUtil.buildVerticalTable(pdfPTableFont, jsonFile, data, aggregateData);

        File pdfFile = new File("C:\\tmp_floder\\out-vertical.pdf");
        writeToFile(pdfFile, table);
    }

    public static List<Map<String, Object>> buildverticalTableData(){
        //构建数据
        List<Map<String, Object>>  data = new ArrayList<>();

        Map<String, Object> map1 = new HashMap<>();
        map1.put("chargeItem", "干线运输-空运费");
        map1.put("chargeObject", "中东宝贝国际物流有限公司");
        map1.put("chargeMoney", "3000.00");

        List<String> costItem = new ArrayList<>();
        costItem.add("干线运输 - 空运费");
        costItem.add("干线运输 - 制单费");
        costItem.add("干线运输 - 人工费");
        map1.put("costItem", costItem);

        List<String> payObject = new ArrayList<>();
        payObject.add("跨时代国际运输服务有限公司");
        payObject.add("飞机国际货运");
        map1.put("payObject", payObject);

        List<String> costMoney = new ArrayList<>();
        costMoney.add("2400.00");
        costMoney.add("300.00");
        map1.put("costMoney", costMoney);

        map1.put("earnings", "300.00");

         Map<String, Object> map2 = new HashMap<>();
        map2.put("chargeItem", "干线运输-提货费");
        map2.put("chargeObject", "");
        map2.put("chargeMoney", "");

        List<Map> costItem2 = new ArrayList<>();

        Map<String, Object> item = new HashMap<>();
        item.put("name", "支线运输-提货费map");
        costItem2.add(item);

        item = new HashMap<>();
        item.put("name", "支线运输-卡车费map");
        costItem2.add(item);
        map2.put("costItem", costItem2);

        List<String> payObject2 = new ArrayList<>();
        payObject2.add("大时代国际运输服务有限公司");
        payObject2.add("北京嘿嘿国际货运代理有限公司");
        map2.put("payObject", payObject2);

        List<String> costMoney2 = new ArrayList<>();
        costMoney2.add("50.00");
        costMoney2.add("150.00");
        map2.put("costMoney", costMoney2);
        map2.put("earnings", "-200.00");

        Map<String, Object> map3 = new HashMap<>();
        map3.put("chargeItem", "报关费");
        map3.put("chargeObject", "中亚宝贝国际物流有限公司");
        map3.put("chargeMoney", "100.00");
        map3.put("costItem", null);
        map3.put("payObject", null);
        map3.put("costMoney", null);
        map3.put("earnings",  null);

        //测试数据
        Map<String, Object> map4 = new HashMap<>();
        map4.put("chargeItem", "测试费用");
        map4.put("chargeObject", "北京特别沉迷于有限公司");
        map4.put("chargeMoney", "100.00");
        map4.put("costItem", null);
        List<String> payObject4 = new ArrayList<>();
        payObject4.add("对象1号");
        payObject4.add("对象2号");
        map4.put("payObject", payObject4);
        map4.put("costMoney", null);
        map4.put("earnings",  null);

        data.add(map1);
        data.add(map2);
        data.add(map3);
        data.add(map4);

        return data;
    }

    public static Map<String, Object> buildverticalAggregateData(){
        Map<String, Object> data = new HashMap<>();
        data.put("chargeMoney", "3100.00");
        data.put("costMoney", "2900.00");
        data.put("earnings", "200.00");
        return data;
    }

    public static void writeToFile(File file, PdfPTable... pdfPTables) throws Exception {

        Document document = new Document(PageSize.A4);
        // 实例化文档对象
        FileOutputStream out = new FileOutputStream(file);
        // 创建写入器
        PdfWriter writer = PdfWriter.getInstance(document, out);

        // 标题的字体
        Font titleFont = new Font(getBaseFont(), 16, Font.BOLD);
        titleFont.setSize(16);

        // 打开文档准备写入内容
        document.open();

        /**标题start***/
        Paragraph paragraph = new Paragraph(
                "\r\nBEIJING  INTERNATIONL HAPPINESS FOWARDING.,LTD\r\n王者荣耀核算单\r\n", titleFont);
        // 设置段落前后间距
        paragraph.setSpacingBefore(0f);

        // setAlignment可以设定段落的对齐方式，setAlignment的参数1为居中对齐、3为右对齐、3为左对齐，默认为左对齐。
        paragraph.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraph);
        /**标题end***/

        //写Table
        for (PdfPTable pdfPTable : pdfPTables) {
            document.add(pdfPTable);
        }

        document.close();
        System.out.println("PDF文件生成于：" + file.getAbsolutePath());
    }


    public static BaseFont getBaseFont() throws Exception {
        String fontPath = PDFJSONUtil.class.getClassLoader().getResource("font/msyh.ttf").getFile();
        BaseFont bfChinese = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        return bfChinese;
    }
}
